package com.gmss.crawler.module.retraction.paper.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gmss.crawler.module.retraction.company.domain.RCompany;
import com.gmss.crawler.module.retraction.paper.domain.RPaper;
import com.gmss.crawler.module.retraction.paper.mapper.RPaperMapper;
import com.gmss.crawler.module.retraction.paper.service.IRPaperService;
import com.gmss.crawler.module.retraction.paperauthor.domain.RPaperAuthor;
import com.gmss.crawler.module.retraction.reason.domain.RPaperReason;
import com.gmss.crawler.utils.DateUtils;
import com.gmss.crawler.utils.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 撤稿论文表 服务实现类
 * </p>
 *
 * @author Auto-generator
 * @since 2021-12-16
 */
@Slf4j
@Service
public class RPaperServiceImpl extends ServiceImpl<RPaperMapper, RPaper> implements IRPaperService {

    @Resource
    private RPaperMapper rPaperMapper;

    @Override
    public List<RPaper> customRPaper() {
        return rPaperMapper.customRPaper();
    }

    @Override
    public boolean writeExcel(String filePath, String fileName, List<RPaperAuthor> authorList) {
        List list = convertExcel(authorList);
        return ExcelUtils.saveExcel(fileName,filePath,list);
    }



//    @Override
//    public boolean writeExcel(String filePath, String fileName, List<RPaper> rPaperList) {
//        List<LinkedHashMap<String, Object>> list = convertExcel(rPaperList);
//        return ExcelUtils.saveExcel(fileName, filePath, list);
//    }

    @Override
    public boolean writeExcelTwo(List<RPaper> rPaperList, Map<Long, List<RPaperReason>> rPaperReasonMap, String filePath, String fileName) {
        List list = convertExcelTwo(rPaperList, rPaperReasonMap);
        return ExcelUtils.saveExcel(fileName,filePath,list);
    }

    public List convertExcel(List<RPaperAuthor> authorList){
        List<LinkedHashMap<String, Object>> rows = new ArrayList<>(authorList.size());
        Object object = authorList.get(0);
        if (object instanceof RPaperAuthor){
            List<RPaperAuthor> list = authorList;
                for (int i = 20000; i < authorList.size() ; i++) {
                    LinkedHashMap<String,Object> row = new LinkedHashMap<>(list.size());
                    row.put("撤稿论文id",authorList.get(i).getRPaperId());
                    row.put("作者",StringUtils.isNotBlank(authorList.get(i).getAuthorName()) ? authorList.get(i).getAuthorName() : "");
                    row.put("作者其他英文名称",StringUtils.isNotBlank(authorList.get(i).getAuthorAltName()) ? authorList.get(i).getAuthorAltName() : "");
                    row.put("作者顺序", authorList.get(i).getAuthorOther() != null ? authorList.get(i).getAuthorOther() : "");
                    row.put("是否是通讯作者",authorList.get(i).getReportAuthor() != null ? authorList.get(i).getReportAuthor() : "");
                    row.put("机构",StringUtils.isNotBlank(authorList.get(i).getRCompanyName()) ? authorList.get(i).getRCompanyName() : "");
                    rows.add(row);
                }
        }
        return rows;
    }

    public List convertExcelTwo(List<RPaper> rPaperList,Map<Long, List<RPaperReason>> rPaperReasonMap){
        List<LinkedHashMap<String, Object>> rows = new ArrayList<>(rPaperList.size());
        Object object = rPaperList.get(0);
        if (object instanceof RPaper){
            List<RPaper> list = rPaperList;
                for (RPaper rPaper : list) {
                    LinkedHashMap<String,Object> row = new LinkedHashMap<>(list.size());
                    row.put("唯一表示",rPaper.getId());
                    row.put("标题", StringUtils.isNotBlank(rPaper.getTitle()) ? rPaper.getTitle() : "");
                    row.put("论文发表日期",StringUtils.isNotBlank(rPaper.getOriginalPublicationDate()) ? rPaper.getOriginalPublicationDate() : "" );
                    row.put("原始doi",StringUtils.isNotBlank(rPaper.getOriginalDoi()) ? rPaper.getOriginalDoi() : "");
                    row.put("原始PubMed",StringUtils.isNotBlank(rPaper.getOriginalPubMedId()) ? rPaper.getOriginalPubMedId() : "");
                    row.put("撤稿或其他通知日期",StringUtils.isNotBlank(rPaper.getRetractionOrOtherNoticesDate()) ? rPaper.getRetractionOrOtherNoticesDate() : "");
                    row.put("撤稿或其他pubMed",StringUtils.isNotBlank(rPaper.getRetractionOrOtherNoticesPubMedId()) ? rPaper.getRetractionOrOtherNoticesPubMedId() : "");
                    row.put("撤稿或其他doi",StringUtils.isNotBlank(rPaper.getRetractionOrOtherNoticesDoi()) ? rPaper.getRetractionOrOtherNoticesDoi() : "");
                    row.put("通知性质",StringUtils.isNotBlank(rPaper.getNatureOfNotice()) ? rPaper.getNatureOfNotice() : "");
                    row.put("期刊",StringUtils.isNotBlank(rPaper.getJournal()) ? rPaper.getJournal() : "");
                    row.put("出版商",StringUtils.isNotBlank(rPaper.getPublisher()) ? rPaper.getPublisher() : "");
                    row.put("学科",StringUtils.isNotBlank(rPaper.getSubject()) ? rPaper.getSubject() : "");
                    row.put("国家",StringUtils.isNotBlank(rPaper.getCountries()) ? rPaper.getCountries() : "");
                    row.put("摘要",StringUtils.isNotBlank(rPaper.getDescribeData()) ? rPaper.getDescribeData() : "");
                    row.put("相关链接",StringUtils.isNotBlank(rPaper.getRelatedLinks()) ? rPaper.getRelatedLinks() : "");
                    List<RPaperReason> rPaperReasons = rPaperReasonMap.get(rPaper.getId());
                    List<String> reasonList = rPaperReasons.stream().filter(rPaperReason -> StringUtils.isNotBlank(rPaperReason.getReason())).map(RPaperReason::getReason).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(reasonList)) {
                        row.put("撤稿原因", StringUtils.join(reasonList, ","));

                    } else {
                        row.put("撤稿原因", "");
                    }
                    rows.add(row);
                }
        }
        return rows;
    }
}
